package com.dianrui.operation.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Window;
import android.widget.ImageView;
import com.dianrui.operation.Constant;
import com.dianrui.operation.adapter.SecretProtocolAdapter;
import com.dianrui.operation.dialog.FlycoDialog.LoadingDialog;
import com.dianrui.operation.util.ActivityManagerUtils;
import com.dianrui.operation.util.GlideUtil;
import com.dianrui.operation.util.SPUtils;
import com.tbit.tbitblesdk.Bike.TbitBle;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.List;
import butterknife.ButterKnife;

/**
 * Activity基类
 */
public abstract class BaseActivity extends AppCompatActivity {
    public SPUtils spUtils;
    private List<ImageView> imageViewList = new ArrayList<>();
    private LoadingDialog mLoadingDialog;
    public WeakReference<BaseActivity> weakReferenceActivity;
    public WeakReference<Context> weakReferenceContext;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);//去除标题栏
        super.onCreate(savedInstanceState);
        setContentView(getContentViewId());
        spUtils = new SPUtils(this, Constant.cfgFileName, Context.MODE_PRIVATE);
        weakReferenceActivity = new WeakReference<>(this);
        weakReferenceContext = new WeakReference<Context>(this);
        ButterKnife.bind(this);//以后没有必要每个界面写一遍butterknife
        TbitBle.initialize(BaseActivity.this, new SecretProtocolAdapter());
        ActivityManagerUtils.getInstance().addActivity(this);
        initView();
    }

    /**
     * 保存图片引用
     *
     * @param imageView
     */
    public void addImageViewList(ImageView imageView) {
        if (imageViewList != null) {
            imageViewList.add(imageView);
        }
    }

    public void showLoadingDialog(String msg) {
        if (mLoadingDialog != null && mLoadingDialog.isShowing()) {
            return;
        }
        if (mLoadingDialog == null) {
            mLoadingDialog = new LoadingDialog(this);
        }
        mLoadingDialog.setMessage(msg);
        mLoadingDialog.setCanceledOnTouchOutside(false);
        mLoadingDialog.setCancelable(false);
        mLoadingDialog.show();
    }

    public void dismissLoadingDialog() {
        if (mLoadingDialog != null && mLoadingDialog.isShowing()) {
            mLoadingDialog.dismiss();
            mLoadingDialog = null;
        }
    }


    /**
     * 清除图片引用
     */
    public void clearImageViewList() {
        if (imageViewList != null) {
            for (int i = 0; i < imageViewList.size(); i++) {
                if (imageViewList.get(i) != null) {
                    GlideUtil.clearImg(this,imageViewList.get(i));
                    imageViewList.set(i, null);
                }
            }
            if (imageViewList != null) {
                imageViewList.clear();
            }
            imageViewList = null;
        }
    }

    protected void JumpActivitys(Class<?> clazz) {
        Intent intent = new Intent(this, clazz);
        startActivity(intent);
    }

    protected void JumpLoginActivitys(Class<?> clazz) {
        Intent intent = new Intent(this, clazz);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK|Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(intent);
    }

    public  void readyGoThenKill(Class<?> clazz) {
        Intent intent = new Intent(this, clazz);
        startActivity(intent);
        finish();
    }




    public abstract int getContentViewId();

    public abstract void initView();

    public abstract void initData();

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (weakReferenceContext != null) {
            weakReferenceContext.clear();
        }
        if (weakReferenceActivity != null) {
            weakReferenceActivity.clear();
        }
        clearImageViewList();
        dismissLoadingDialog();
        ActivityManagerUtils.getInstance().finishActivity(this);
    }
}
